#!/usr/bin/env python
#
# This file is part of Astir
# 
# Astir is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Astir is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Astir.  If not, see <http://www.gnu.org/licenses/>.
#
# Astir Copyright (C) 2008 Julien Bert 

# V0.2 2008-12-22 15:09:34 JB
lines = open('pymir_kernel.py', 'r').readlines()
#lines = open('astir_shell.py', 'r').readlines()
f       = open('README', 'w')
mino    = 0
tsp     = [0, 0, 0] 
authors = []
mem     = ''

for nl in xrange(len(lines)):
    line = lines[nl]
    if line[:3] == 'def':
        func = line[4:].strip('\n:').split('(')[0]
        data = lines[nl - 1].strip('\n').split()
        if len(data) == 5:
            mino    += int(data[1].split('.')[-1])
            y, m, d  = data[2].split('-')
            y, m, d  = int(y), int(m), int(d)
            if y > tsp[0] or m > tsp[1] or d > tsp[2]: tsp = [y, m, d]
            if data[4] not in authors: authors.append(data[4])
            mem += (data[1] + ' ' + func + '\n')
        else:
            mem += ('VX.X ' + func + '\n')
    elif line[:3] == '## ':
        mem += ('\n' + line)

majo = mino // 100
mino = mino % 100
authors.sort()
txt = ''
for author in authors: txt += (author + ', ')

f.write('********************************\n')
f.write('* pymir_kernel.py     VERSION  *\n')
f.write('********************************\n')
f.write('* Version: %d.%d\n' % (majo, mino))
f.write('* Date   : %4d-%02d-%02d\n' % (tsp[0], tsp[1], tsp[2]))
f.write('* Authors: %s\n' % author)

f.write('%s\n' % mem)

f.write('********************************\n')
f.write('*        DOCUMENTATION         *\n')
f.write('********************************\n\n')

nl = 0
while nl != len(lines):
    if lines[nl][:3] == '## ':
        f.write('%s' % lines[nl])
    elif lines[nl][:3] == 'def' and lines[nl + 1].find('\'\'\'') != -1:
        f.write('%s' % lines[nl][4:])
        nl += 2
        while lines[nl].find('\'\'\'') == -1:
            f.write('%s' % lines[nl])
            nl += 1
        f.write('\n')

    nl += 1

import time
f.write('-=: Generated by pypack %s :=-\n' % time.ctime())
f.close()

'''
import tarfile, os
name = 'package_V%d.%d_%04d%02d%02d.tar' % (majo, mino, tsp[0], tsp[1], tsp[2])
t = tarfile.open(name, 'w')
t.add('uwat_util.py')
t.add('README')
t.close()
os.system('mv %s PCKVS/' % name)
print name, '    [ok]'
'''
